package com.kzm.controller;

import com.kzm.common.exception.ConflictException;
import com.kzm.common.exception.ForbiddenException;
import com.kzm.configuration.properties.BannerProperties;
import com.kzm.entity.Player;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
@RequestMapping("index")
public class IndexController implements EnvironmentAware {

    private static final Logger logger = LoggerFactory.getLogger(IndexController.class);

    private final Player player;

    //通过@Value去读取配置文件的
    @Value("${player.name}")
    private String playerName;

    private final BannerProperties bannerProperties;

    public IndexController(BannerProperties bannerProperties) {
        this.bannerProperties = bannerProperties;
        this.player = new Player();
    }


    @ResponseBody
    @RequestMapping("test")
    public String test() {
        player.setName(playerName);
        logger.info("banner => {}", bannerProperties);
        logger.info("player => {}", player);
        return "indxe/test";
    }


    @RequestMapping("hello")
    public String hello() {
        return "hello";
    }

    @RequestMapping("forbidden")
    public String forbidden() {
        throw new ForbiddenException();
    }

    @RequestMapping("conflict")
    public String conflict() {
        throw new ConflictException();
    }


    @Override
    public void setEnvironment(Environment environment) {
        //通过实现接口EnvironmentAware去读取配置文件的值
        String playerId = environment.getProperty("player.id");
        player.setId(Integer.valueOf(playerId));
    }
}
